package com.virtium.vpc.dao.hibernate;

import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Criteria;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.Transaction;
import org.hibernate.criterion.Example;
import org.hibernate.criterion.Order;

import com.virtium.vpc.dao.VpcProductDAO;
import com.virtium.vpc.hibernate.model.VpcProduct;

/**
 * A data access object (DAO) providing persistence and search support for
 * VpcProduct entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see com.virtium.vpc.hibernate.model.VpcProduct
 * @author NguyenTuanAnh
 */

public class VpcProductDAOHibernate extends BaseHibernateDAO implements VpcProductDAO {
	private static final Log log = LogFactory.getLog(VpcProductDAOHibernate.class);
	// property constants
	public static final String PN = "pn";
	public static final String CONGIGURATION_ID = "congigurationId";
	public static final String DENSITY_ID = "densityId";
	public static final String MODULERANK_ID = "modulerankId";
	public static final String PACKAGE_ID = "packageId";
	public static final String PRODUCTSTATUS_ID = "productstatusId";
	public static final String TECHNOLOGY_ID = "technologyId";
	public static final String FORM_FACTOR_ID = "formFactorId";
	public static final String RAMDENSITY_ID = "ramdensityId";
	public static final String HEIGHT_ID = "heightId";
	public static final String THICKNESS_ID = "thicknessId";
	public static final String SPEED_ID = "speedId";
	public static final String SPECIALTY = "specialty";
	public static final String DATA_SHEET = "dataSheet";
	public static final String PRODUCT_BRIEF = "productBrief";
	public static final String RUGGED_MEMORY = "ruggedMemory";
	public static final String HIGH_TEMP = "highTemp";

	public boolean save(VpcProduct transientInstance) {
		log.debug("saving VpcProduct instance");
		try {
			Transaction tx = getSession().beginTransaction();
			hsession.save(transientInstance);
			tx.commit();
			
			log.debug("save successful");
			return true;
		} catch (RuntimeException re) {
			log.error("save failed", re);
			//throw re;
		}
		return false;
	}

	public void delete(VpcProduct persistentInstance) {
		log.debug("deleting VpcProduct instance");
		try {
			Transaction tx = getSession().beginTransaction();
			hsession.delete(persistentInstance);
			tx.commit();
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}/*finally {
			closeSession();
		}*/
	}
	
	public boolean update(VpcProduct persistentInstance) {
		log.debug("deleting VpcProduct instance");
		try {
			Transaction tx = getSession().beginTransaction();
			hsession.update(persistentInstance);
			tx.commit();
			log.debug("delete successful");
			return true;
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			//throw re;
		}/*finally {
			closeSession();
		}*/
		return false;
	}

	public VpcProduct findById(java.lang.Integer id) {
		log.debug("getting VpcProduct instance with id: " + id);
		try {
			VpcProduct instance = (VpcProduct) getSession().get(
					"com.virtium.vpc.hibernate.model.VpcProduct", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(VpcProduct instance) {
		log.debug("finding VpcProduct instance by example");
		try {
			List results = getSession().createCriteria("com.virtium.vpc.hibernate.model.VpcProduct").add(
					Example.create(instance)).list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding VpcProduct instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from VpcProduct as model where model."
					+ propertyName + "= ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByPn(Object pn) {
		return findByProperty(PN, pn);
	}

	public List findByCongigurationId(Object congigurationId) {
		return findByProperty(CONGIGURATION_ID, congigurationId);
	}

	public List findByDensityId(Object densityId) {
		return findByProperty(DENSITY_ID, densityId);
	}

	public List findByModulerankId(Object modulerankId) {
		return findByProperty(MODULERANK_ID, modulerankId);
	}

	public List findByPackageId(Object packageId) {
		return findByProperty(PACKAGE_ID, packageId);
	}

	public List findByProductstatusId(Object productstatusId) {
		return findByProperty(PRODUCTSTATUS_ID, productstatusId);
	}

	public List findByTechnologyId(Object technologyId) {
		return findByProperty(TECHNOLOGY_ID, technologyId);
	}

	public List findByFormFactorId(Object formFactorId) {
		return findByProperty(FORM_FACTOR_ID, formFactorId);
	}

	public List findByRamdensityId(Object ramdensityId) {
		return findByProperty(RAMDENSITY_ID, ramdensityId);
	}

	public List findByHeightId(Object heightId) {
		return findByProperty(HEIGHT_ID, heightId);
	}

	public List findByThicknessId(Object thicknessId) {
		return findByProperty(THICKNESS_ID, thicknessId);
	}

	public List findBySpeedId(Object speedId) {
		return findByProperty(SPEED_ID, speedId);
	}

	public List findBySpecialty(Object specialty) {
		return findByProperty(SPECIALTY, specialty);
	}

	public List findByDataSheet(Object dataSheet) {
		return findByProperty(DATA_SHEET, dataSheet);
	}

	public List findByProductBrief(Object productBrief) {
		return findByProperty(PRODUCT_BRIEF, productBrief);
	}

	public List findByRuggedMemory(Object ruggedMemory) {
		return findByProperty(RUGGED_MEMORY, ruggedMemory);
	}

	public List findByHighTemp(Object highTemp) {
		return findByProperty(HIGH_TEMP, highTemp);
	}

	public List findAll() {
		log.debug("finding all VpcProduct instances");
		try {
			String queryString = "from VpcProduct";
			Query queryObject = getSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public VpcProduct merge(VpcProduct detachedInstance) {
		log.debug("merging VpcProduct instance");
		try {
			VpcProduct result = (VpcProduct) getSession().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(VpcProduct instance) {
		log.debug("attaching dirty VpcProduct instance");
		try {
			getSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(VpcProduct instance) {
		log.debug("attaching clean VpcProduct instance");
		try {
			getSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}
	
	public List queryString(String queryString){		
		try {
			
			Query queryObject = getSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}
}